import { DECREMENT, INCREMENT } from "../const/counter.const"
import { handleActions as createReducer} from 'redux-actions'
import { decrement, increment } from "../actions/counter.actions"

const initialState = {
    count: 0,
}
// 函数中带有两个形参 state action
const handleIncrement = (state, action) => ({count: state.count + action.payload})
const handleDecrement = (state, action) => ({count: state.count - action.payload})

// createReducer 的返回值就是 reducer 函数，接收一个对象，对象的键是 action ，值是函数
export default createReducer({
    [increment]: handleIncrement,
    [decrement]: handleDecrement
}, initialState)

// export default (state = initialState, action) => {
//     switch (action.type) {
//         case INCREMENT:
//             return {
//                 ...state,
//                 count: state.count + action.payload
//             }
//         case DECREMENT:
//             return {
//                 ...state,
//                 count: state.count - action.payload
//             }
//         default:
//             return state
//     }
// }